package huawei.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources.Theme;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.KeyEvent.DispatcherState;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.SearchView.SearchAutoComplete;
import huawei.android.widget.DecouplingUtil.ReflectUtil;
import java.lang.reflect.InvocationTargetException;
import o.ax;
import o.fo;
import o.fqc.a;
import o.fqc.b;
import o.fqc.c;
import o.fqc.e;
import o.fqc.f;
import o.fqc.g;
import o.gg;

public class HwSearchView extends SearchView {
    private static final String ͺ = HwSearchView.class.getSimpleName();
    private View ˋॱ;
    private int ˏॱ;
    private HwSearchAutoComplete ॱˊ;

    interface d {
        void ˊ(int[] iArr);
    }

    @SuppressLint({"RestrictedApi"})
    public static class HwSearchAutoComplete extends SearchAutoComplete {
        private final Drawable ʻ = fo.ˊ(getContext(), a.ॱ);
        private final int ʽ = getResources().getDimensionPixelSize(c.ˎ);
        private d ˊ;
        private View ˋ;
        private int ˎ;
        private View ॱ;

        @SuppressLint({"RestrictedApi"})
        public HwSearchAutoComplete(Context context) {
            super(context);
        }

        @SuppressLint({"RestrictedApi"})
        public HwSearchAutoComplete(Context context, AttributeSet attributeSet) {
            super(context, attributeSet);
        }

        @SuppressLint({"RestrictedApi"})
        public HwSearchAutoComplete(Context context, AttributeSet attributeSet, int i) {
            super(context, attributeSet, i);
        }

        protected void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            super.onTextChanged(charSequence, i, i2, i3);
            ˋ(charSequence);
            ˊ(TextUtils.isEmpty(charSequence) ? this.ʽ : 0);
        }

        public void drawableStateChanged() {
            super.drawableStateChanged();
            if (this.ˊ != null) {
                this.ˊ.ˊ(getDrawableState());
            }
        }

        public boolean onKeyPreIme(int i, KeyEvent keyEvent) {
            if (i != 4) {
                return false;
            }
            if (keyEvent.getAction() == 0 && keyEvent.getRepeatCount() == 0) {
                DispatcherState keyDispatcherState = getKeyDispatcherState();
                if (keyDispatcherState != null) {
                    keyDispatcherState.startTracking(keyEvent, this);
                }
                return true;
            } else if (keyEvent.getAction() != 1) {
                return false;
            } else {
                DispatcherState keyDispatcherState2 = getKeyDispatcherState();
                if (keyDispatcherState2 != null) {
                    keyDispatcherState2.handleUpEvent(keyEvent);
                }
                if (!keyEvent.isTracking() || keyEvent.isCanceled()) {
                    return false;
                }
                boolean hideSoftInputFromWindow;
                if (isPopupShowing()) {
                    dismissDropDown();
                }
                ˎ();
                InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService("input_method");
                if (inputMethodManager != null) {
                    hideSoftInputFromWindow = inputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
                } else {
                    hideSoftInputFromWindow = false;
                }
                return hideSoftInputFromWindow;
            }
        }

        private void ˎ() {
            try {
                TextView.class.getDeclaredMethod("stopTextActionMode", new Class[0]).invoke(this, new Object[0]);
            } catch (IllegalAccessException e) {
                Log.e(HwSearchView.ͺ, "illegal access stopTextActionMode");
            } catch (InvocationTargetException e2) {
                Log.e(HwSearchView.ͺ, "invocation stopTextActionMode error");
            } catch (NoSuchMethodException e3) {
                Log.e(HwSearchView.ͺ, "method stopTextActionMode not found");
            }
        }

        private void ˊ(int i) {
            if (this.ॱ != null && i != this.ॱ.getPaddingEnd()) {
                if (this.ˋ != null && this.ˋ.getVisibility() == 0) {
                    i = 0;
                }
                this.ॱ.setPaddingRelative(this.ॱ.getPaddingStart(), this.ॱ.getPaddingTop(), i, this.ॱ.getPaddingBottom());
            }
        }

        private void ˋ(CharSequence charSequence) {
            if (this.ˋ != null) {
                this.ˋ.setVisibility(TextUtils.isEmpty(charSequence) ? 0 : 8);
            }
        }

        private void ॱ(d dVar) {
            this.ˊ = dVar;
        }

        private void ˊ(View view) {
            this.ॱ = view;
        }

        private void ˋ(View view) {
            this.ˋ = view;
            ˊ(0);
        }

        private void ˋ(int i) {
            this.ˎ = i;
            ʽ();
        }

        private void ʽ() {
            try {
                Class cls = TextView.class;
                Object object = ReflectUtil.getObject(this, "mEditor", cls);
                Class cls2 = Class.forName("android.widget.Editor");
                if (VERSION.SDK_INT >= 28) {
                    ReflectUtil.callMethod(object, "updateCursorPosition", null, null, cls2);
                    try {
                        Drawable ˋ = ˋ((Drawable) cls.getDeclaredMethod("getTextCursorDrawable", new Class[0]).invoke(this, new Object[0]), this.ˎ);
                        cls.getDeclaredMethod("setTextCursorDrawable", new Class[]{Drawable.class}).invoke(this, new Object[]{ˋ});
                        return;
                    } catch (NoSuchMethodException e) {
                        ReflectUtil.setObject("mDrawableForCursor", object, ˋ((Drawable) ReflectUtil.getObject(object, "mDrawableForCursor", cls2), this.ˎ), cls2);
                        return;
                    }
                }
                Drawable[] drawableArr = (Drawable[]) ReflectUtil.getObject(object, "mCursorDrawable", cls2);
                if (drawableArr != null) {
                    for (int i = 0; i < drawableArr.length; i++) {
                        drawableArr[i] = ˋ(drawableArr[i], this.ˎ);
                    }
                }
                ReflectUtil.setObject("mCursorDrawable", object, drawableArr, cls2);
            } catch (IllegalAccessException e2) {
                Log.e(HwSearchView.ͺ, "illegal access");
            } catch (InvocationTargetException e3) {
                Log.e(HwSearchView.ͺ, "invocation error");
            } catch (ClassNotFoundException e4) {
                Log.e(HwSearchView.ͺ, "class not found");
            }
        }

        private Drawable ˋ(Drawable drawable, int i) {
            if (drawable == null) {
                if (this.ˎ == 0 || this.ʻ == null) {
                    return null;
                }
                drawable = this.ʻ.mutate();
            }
            Drawable mutate = gg.ʻ(drawable).mutate();
            gg.ॱ(mutate, i);
            return mutate;
        }
    }

    public HwSearchView(Context context) {
        this(context, null);
    }

    public HwSearchView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, e.ˏ);
    }

    public HwSearchView(Context context, AttributeSet attributeSet, int i) {
        super(ˋ(context), attributeSet, i);
        this.ॱˊ = (HwSearchAutoComplete) findViewById(o.fqc.d.ˋ);
        this.ˋॱ = findViewById(o.fqc.d.ˏ);
        this.ॱˊ.ˊ(this.ˋॱ);
        this.ॱˊ.ˋ(findViewById(o.fqc.d.ˎ));
        TypedArray obtainStyledAttributes = super.getContext().obtainStyledAttributes(attributeSet, g.ˋˊ, i, f.ˊ);
        ˎ(obtainStyledAttributes.getColor(g.ˊˊ, 0));
        this.ॱˊ.ˋ(obtainStyledAttributes.getColor(g.ˉ, fo.ˋ(context, b.ॱ)));
        this.ॱˊ.ॱ(new d(this) {
            final /* synthetic */ HwSearchView ˎ;

            {
                this.ˎ = r1;
            }

            public void ˊ(int[] iArr) {
                Drawable background = this.ˎ.ˋॱ.getBackground();
                if (background != null) {
                    background.setState(iArr);
                }
            }
        });
        obtainStyledAttributes.recycle();
        this.ॱˊ.setText(this.ॱˊ.getText());
    }

    public void onMeasure(int i, int i2) {
        if (getOrientation() == 1) {
            ˎ("measureVertical", i, i2);
        } else {
            ˎ("measureHorizontal", i, i2);
        }
    }

    private void ˎ(String str, int i, int i2) {
        ReflectUtil.callMethod(this, str, new Class[]{Integer.TYPE, Integer.TYPE}, new Object[]{Integer.valueOf(i), Integer.valueOf(i2)}, LinearLayoutCompat.class);
    }

    private static Context ˋ(Context context) {
        Theme theme = context.getTheme();
        theme.applyStyle(f.ˎ, false);
        theme.applyStyle(f.ॱ, true);
        return new ax(context, theme);
    }

    public void setSearchButtonTextColor(int i) {
        ˎ(i);
    }

    private void ˎ(int i) {
        if (this.ˏॱ != i) {
            View findViewById = findViewById(o.fqc.d.ॱ);
            if (findViewById instanceof TextView) {
                ((TextView) findViewById).setTextColor(i);
                this.ˏॱ = i;
            }
        }
    }

    public void setTextCursorColor(int i) {
        if (this.ॱˊ.ˎ != i) {
            this.ॱˊ.ˋ(i);
        }
    }
}
